﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;using THSERP.BusinessEntities;using THSERP.BaseProvider;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraCharts;
using THSLib;using THSERP.BaseProvider;

namespace THSERP.Modules.Report
{
    public partial class RP006 : ReportForm
    {
        private String[] DayOfWeeks =  { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"};
        private String[] Months = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
        private enum FilterType { DayOfWeek, DayOfMonth, Month, Year };
        private FilterType filterType;
        private THSList<ARInvoicesInfo> lstARInvoice;
        private List<int> lstExistYear;

        public RP006()
        {
            InitializeComponent();
            lstARInvoice = new THSList<ARInvoicesInfo>();
            lstExistYear = new List<int>();
        }

        private void fld_btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void RP006_Load(object sender, EventArgs e)
        {
            fld_dteSearchFromARInvoiceDate.DateTime = THSApp.WorkingDate;
            fld_dteSearchToARInvoiceDate.DateTime = THSApp.WorkingDate;
            filterType = FilterType.DayOfWeek;
            ViewSaleOverTimeChart(filterType);
            //fld_lblBranchName.Text = ((BRBranchsInfo)new BRBranchsController().GetObjectByID(THSApp.CurrentCompanyInfo.FK_BRBranchID)).BRBranchName;
        }

        private void ViewSaleOverTimeChart(FilterType type)
        {
            lstARInvoice.Clear();
            StringBuilder queryBuilder = new StringBuilder();
            queryBuilder.Append(GenerateSearchQuery(THSUtil.GetTableNameFromBusinessObjectType(typeof(ARInvoicesInfo))));
            queryBuilder.Append(String.Format("AND FK_BRBranchID = {0}", THSApp.CurrentCompanyInfo.FK_BRBranchID));
            ARInvoicesController objInvoicesController = new ARInvoicesController();
            DataSet ds = objInvoicesController.GetDataSet(queryBuilder.ToString());
            fld_chcSaleOverTime.Series[0].Points.Clear();

            foreach(DataRow row in ds.Tables[0].Rows)
            {
                ARInvoicesInfo objInvoicesInfo = (ARInvoicesInfo)objInvoicesController.GetObjectFromDataRow(row);
                lstARInvoice.Add(objInvoicesInfo);
            }

            List<double> lstInvoiceTotalAmount = new List<double>();
            switch(type)
            {
                case FilterType.DayOfWeek:
                    lstInvoiceTotalAmount = GetInvoiceTotalAmountByDayOfWeek(lstARInvoice);
                    break;
                case FilterType.DayOfMonth:
                    lstInvoiceTotalAmount = GetInvoiceTotalAmountByDayOfMonth(lstARInvoice);
                    break;
                case FilterType.Month:
                    lstInvoiceTotalAmount = GetInvoiceTotalAmountByMonth(lstARInvoice);
                    break;
                case FilterType.Year:
                    lstInvoiceTotalAmount = GetInvoiceTotalAmountByYear(lstARInvoice);
                    break;
            }

            for (int i = 0; i < lstInvoiceTotalAmount.Count; i++)
            {
                lstInvoiceTotalAmount[i] = Math.Round(lstInvoiceTotalAmount[i], 2);
            }

            SeriesPoint point = new SeriesPoint();
            for(int i = 0; i < lstInvoiceTotalAmount.Count; i++)
            {
                switch (type)
                {
                    case FilterType.DayOfWeek:
                        point = new SeriesPoint(DayOfWeeks[i], lstInvoiceTotalAmount[i]);
                        break;
                    case FilterType.DayOfMonth:
                        point = new SeriesPoint((i + 1).ToString(), lstInvoiceTotalAmount[i]);
                        break;
                    case FilterType.Month:
                        point = new SeriesPoint(Months[i], lstInvoiceTotalAmount[i]);
                        break;
                    case FilterType.Year:
                        point = new SeriesPoint(lstExistYear[i].ToString(), lstInvoiceTotalAmount[i]);
                        break;
                }
                fld_chcSaleOverTime.Series[0].Points.Add(point);
            }
        }

        private List<double> GetInvoiceTotalAmountByDayOfWeek(THSList<ARInvoicesInfo> lstInvoice)
        {
            List<double> lstInvoiceTotalAmount = InitList(7);

            foreach (ARInvoicesInfo objInvoiceInfo in lstInvoice)
            {
                switch (objInvoiceInfo.ARInvoiceDate.DayOfWeek)
                {
                    case DayOfWeek.Monday:
                        lstInvoiceTotalAmount[0] += objInvoiceInfo.ARInvoiceTotalAmount;
                        break;
                    case DayOfWeek.Tuesday:
                        lstInvoiceTotalAmount[1] += objInvoiceInfo.ARInvoiceTotalAmount;
                        break;
                    case DayOfWeek.Wednesday:
                        lstInvoiceTotalAmount[2] += objInvoiceInfo.ARInvoiceTotalAmount;
                        break;
                    case DayOfWeek.Thursday:
                        lstInvoiceTotalAmount[3] += objInvoiceInfo.ARInvoiceTotalAmount;
                        break;
                    case DayOfWeek.Friday:
                        lstInvoiceTotalAmount[4] += objInvoiceInfo.ARInvoiceTotalAmount;
                        break;
                    case DayOfWeek.Saturday:
                        lstInvoiceTotalAmount[5] += objInvoiceInfo.ARInvoiceTotalAmount;
                        break;
                    case DayOfWeek.Sunday:
                        lstInvoiceTotalAmount[6] += objInvoiceInfo.ARInvoiceTotalAmount;
                        break;
                }
            }
            return lstInvoiceTotalAmount;
        }

        private List<double> GetInvoiceTotalAmountByDayOfMonth(THSList<ARInvoicesInfo> lstInvoice)
        {
            List<double> lstInvoiceTotalAmount = InitList(31);

            foreach (ARInvoicesInfo objInvoicesInfo in lstInvoice)
            {
                int orderDay = objInvoicesInfo.ARInvoiceDate.Day;
                lstInvoiceTotalAmount[orderDay - 1] += objInvoicesInfo.ARInvoiceTotalAmount;
            }
            return lstInvoiceTotalAmount;
        }

        private List<double> GetInvoiceTotalAmountByMonth(THSList<ARInvoicesInfo> lstInvoice)
        {
            List<double> lstInvoiceTotalAmount = InitList(12);

            foreach (ARInvoicesInfo objInvoicesInfo in lstInvoice)
            {
                int orderMonth = objInvoicesInfo.ARInvoiceDate.Month;
                lstInvoiceTotalAmount[orderMonth - 1] += objInvoicesInfo.ARInvoiceTotalAmount;
            }
            return lstInvoiceTotalAmount;
        }

        private List<double> GetInvoiceTotalAmountByYear(THSList<ARInvoicesInfo> lstInvoice)
        {
            foreach (ARInvoicesInfo objInvoicesInfo in lstInvoice)
            {
                int orderYear = objInvoicesInfo.ARInvoiceDate.Year;
                if (lstExistYear.Count == 0 || lstExistYear.Contains(orderYear) == false)
                    lstExistYear.Add(orderYear);
            }
            lstExistYear.Sort();
            List<double> lstInvoiceTotalAmount = InitList(lstExistYear.Count);

            foreach(ARInvoicesInfo objInvoicesInfo in lstInvoice)
            {
                int orderYear = objInvoicesInfo.ARInvoiceDate.Year;
                lstInvoiceTotalAmount[lstExistYear.IndexOf(orderYear)] += objInvoicesInfo.ARInvoiceTotalAmount;
            }

            return lstInvoiceTotalAmount;
        }

        private void fld_ctxItemDayOfWeek_Click(object sender, EventArgs e)
        {
            fld_lblFilter.Text = fld_ctxItemDayOfWeek.Text;
            filterType = FilterType.DayOfWeek;
            ViewSaleOverTimeChart(filterType);
        }

        private void fld_ctxItemDayOfMonth_Click(object sender, EventArgs e)
        {
            fld_lblFilter.Text = fld_ctxItemDayOfMonth.Text;
            filterType = FilterType.DayOfMonth;
            ViewSaleOverTimeChart(filterType);
        }

        private void fld_ctxItemMonth_Click(object sender, EventArgs e)
        {
            fld_lblFilter.Text = fld_ctxItemMonth.Text;
            filterType = FilterType.Month;
            ViewSaleOverTimeChart(filterType);
        }

        private void fld_ctxItemYear_Click(object sender, EventArgs e)
        {
            fld_lblFilter.Text = fld_ctxItemYear.Text;
            filterType = FilterType.Year;
            ViewSaleOverTimeChart(filterType);
        }

        private void fld_btnApply_Click(object sender, EventArgs e)
        {
            ViewSaleOverTimeChart(filterType);
        }

        private List<double> InitList(int numElement)
        {
            List<double> list = new List<double>(numElement);
            for (int i = 0; i < numElement; i++)
                list.Add(0);
            return list;
        }

        private void fld_btnPrint_Click(object sender, EventArgs e)
        {
            // fld_cctrlARRP005InvoiceItems.Print(DevExpress.XtraCharts.Printing.PrintSizeMode.None);
            //this.fld_dgcARRP002InvoiceItems.PrintPriview();
            DevExpress.XtraPrinting.PrintingSystem printingSystem = new DevExpress.XtraPrinting.PrintingSystem();
            DevExpress.XtraPrinting.PrintableComponentLink printLink = new DevExpress.XtraPrinting.PrintableComponentLink();

            try
            {
                //Set to landscape
                printingSystem.PageSettings.Landscape = true;
                this.Cursor = Cursors.WaitCursor;
                printLink.Component = this.fld_chcSaleOverTime;
                printLink.CreateDocument(printingSystem);
                printLink.VerticalContentSplitting = DevExpress.XtraPrinting.VerticalContentSplitting.Exact;
                printingSystem.PreviewFormEx.ShowDialog();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            finally
            {
                this.Cursor = Cursors.Default;
                printingSystem.Dispose();
                printLink.Dispose();
            }
        }

        private void fld_btnExcel_Click(object sender, EventArgs e)
        {
            SaveFileDialog dlgSave = new SaveFileDialog();
            dlgSave.Filter = "Microsoft Excel|*.xls";
            DialogResult dlgResult = dlgSave.ShowDialog();
            if (dlgResult == DialogResult.OK)
            {
                Cursor currentCursor = Cursor.Current;
                Cursor.Current = Cursors.WaitCursor;
                // this.fld_dgcARRP002InvoiceItems.ExportToXls(dlgSave.FileName);
                this.FindForm().Refresh();
                this.fld_chcSaleOverTime.ExportToXls(dlgSave.FileName);
                Cursor.Current = currentCursor;
                if (DialogResult.Yes == MessageBox.Show("Xuất file Excel thành công! Bạn có muốn mở file ?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question))
                {
                    System.Diagnostics.Process.Start(dlgSave.FileName);
                }
            }
        }
    }
}